<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台统计汇总模型
 * @author 66diqiu-hdk
 *
 */
class Admin_Datan_model extends CI_Model {

    /**
     * @var 表名称
     */
    private $sid;


    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('admin.area_model');
    }

    /**
     * 订单产品排行
     * @param int $start
     * @param int $end
     */
    function orderline_rank($start=0,$end=0){
        $table = $this->db->dbprefix("order_go_date");
        $where = " sid=$this->sid ";
        if($start){
            $where .= " and go_date>='".$start."' ";
        }
        if($end){
            $where .= " and go_date<='".$end."' ";
        }

        $sql   = "select line_id,line_name,count(line_id) as num from $table where $where GROUP BY line_id ORDER BY num desc LIMIT 10 ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 出行目的地排行
     * @param int $start
     * @param int $end
     * @param int $type 1省2市
     *
     */
    function dest_rank($start=0,$end=0,$type=0){
        $table = $this->db->dbprefix("order_go_date");
        $table_line = $this->db->dbprefix("line");

        $where = " a.sid=$this->sid ";
        if($start){
            $where .= " and a.go_date>='".$start."' ";
        }
        if($end){
            $where .= " and a.go_date<='".$end."' ";
        }

        $sql   = "select b.d_province as dest,count(b.d_province) as num from $table as a left join $table_line as b on b.line_id=a.line_id where $where and b.d_province>0  GROUP BY b.d_province ORDER BY num desc LIMIT 10 ";
        if($type==1){
            $sql   = "select b.d_city as dest,count(b.d_city) as num from $table as a left join $table_line as b on b.line_id=a.line_id where $where and b.d_city>0  GROUP BY b.d_city ORDER BY num desc LIMIT 10 ";
        }
        $query = $this->db->query($sql);
        $list =  $query->result_array();
        foreach($list as $key=>$val){
            @$list[$key]['dest_name'] = trim($this->area_model->get_area_name(array($val['dest'])), '[]');
        }
        return $list;

    }

    function tour_level($start=0,$end=0){
        $table_order = $this->db->dbprefix("order_info");
        $table_order_go = $this->db->dbprefix("order_go_date");
        $sql = "select count(a.order_id) as num from $table_order as a LEFT JOIN  $table_order_go as b on b.order_id=a.order_id where b.total_num>0 and a.sid=$this->sid ";
        if($start>0){
            $sql.= " and (a.order_account/b.total_num)>$start ";
        }
        if($end>0){
            $sql.= " and (a.order_account/b.total_num)<$end ";
        }
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     * 订单类型的数量
     * @param int $cat 1国内2周边3出境
     */
    function line_cat($cat=1){
        $table_order = $this->db->dbprefix("order_go_date");
        $table_line = $this->db->dbprefix("line");
        $sql = "select count(a.order_id) as num from $table_order as a left join $table_line as b on b.line_id=a.line_id where b.cat_id=$cat and a.sid=$this->sid ";
        $row = $this->db->query($sql)->row_array($sql);
        return $row['num'];
    }

    function yeji($format = "'%Y%m%d'", $where = ''){
        $table_order = $this->db->dbprefix('order_info');
        $sql = "select DATE_FORMAT(add_time,$format) as date,sum(order_account) as total_account,sum(paid_account) as total_paid from " . $table_order;
        $sql .= " WHERE sid=$this->sid and order_type=1 ";
        $sql .= $where ? " AND $where" : '';
        $sql .= " group by DATE_FORMAT(add_time,$format) ORDER BY date ASC";
        $query = $this->db->query($sql);
        $list = $query->result_array();
        $return  = array();
        foreach($list as $key=>$val){
            $return[$val[date]][total_account] = $val['total_account'];
            $return[$val[date]][total_paid] = $val['total_paid'];
        }
        return $return;
    }

}